import { _decorator, Component, Node, Size, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('aabb')
export class aabb extends Component {

    @property(Node)
    private requiredNode: Node = null;

    @property(Node)
    private requiredNode2: Node = null;

    private position = new Vec3();
    private size = new Size();
    private leftBot = new Vec3();
    private rightTop = new Vec3();

    private position2 = new Vec3();
    private size2 = new Size();
    private leftBot2 = new Vec3();
    private rightTop2 = new Vec3();


    start() {
        this.position = this.requiredNode.position;
        this.size = this.requiredNode.getComponent(UITransform).contentSize;
        this.leftBot = new Vec3(this.position.x-this.size.width/2, this.position.y-this.size.height/2, 0);
        this.rightTop = new Vec3(this.position.x+this.size.width/2, this.position.y+this.size.height/2, 0);

        this.position2 = this.requiredNode2.position;
        this.size2 = this.requiredNode2.getComponent(UITransform).contentSize;
        this.leftBot2 = new Vec3(this.position2.x-this.size2.width/2, this.position2.y-this.size2.height/2, 0);
        this.rightTop2 = new Vec3(this.position2.x+this.size2.width/2, this.position2.y+this.size2.height/2, 0);
    }

    checkCollision(): boolean {
        if (this.leftBot.x > this.rightTop2.x || this.leftBot2.x > this.rightTop.x) return false;
        if (this.leftBot.y > this.rightTop2.y || this.leftBot2.y > this.rightTop.y) return false;
        return true;
    }

    update(deltaTime: number) {
        console.log(this.checkCollision());

        this.leftBot = new Vec3(this.position.x-this.size.width/2, this.position.y-this.size.height/2, 0);
        this.rightTop = new Vec3(this.position.x+this.size.width/2, this.position.y+this.size.height/2, 0);
        this.leftBot2 = new Vec3(this.position2.x-this.size2.width/2, this.position2.y-this.size2.height/2, 0);
        this.rightTop2 = new Vec3(this.position2.x+this.size2.width/2, this.position2.y+this.size2.height/2, 0);
    }
}


